package lucky.xiong.实际工作问题;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

public class 生成测试JSON数据 {

    public static void main(String[] args) {
        lizhiUser();
        zaizhiUser();
//        listOu();
    }

    private static void zaizhiUser() {
        // 定义基础数据模板
        JSONObject template = new JSONObject();
        template.put("ASSIGNMENT_ID", null);
        template.put("ACTUAL_TERMINATION_DATE", "2012-12-31T00:00:00.00000000");
        template.put("LEAVING_REASON", "WT");
        template.put("BUSINESS_GROUP_ID", "82");
        template.put("LAST_MODIFY_TM", "2020-02-28T00:00:00.00000000");
//        template.put("SEX", "男");
//        template.put("PERSON_TYPE", "离职人员");
        template.put("POSITION_STATUS", null);
        template.put("LAST_UPDATE_DATE", "2020-02-28T00:00:00.00000000");
        template.put("MAILSTOP", "example@domain.com");
        template.put("EFFECTIVE_END_DATE", "4712-12-31T00:00:00");
        template.put("EFFECTIVE_START_DATE", "2020-12-31T00:00:00");
        template.put("IDM_EMP_ACCOUNT", "HUCHONGMING");

        // 生成测试数据
        JSONArray employeeArray = new JSONArray();
        Set<String> employeeNumbers = new HashSet<>();
        Set<String> personIds = new HashSet<>();

        int totalRecords = 11; // 需要生成的记录数
        Random random = new Random();
        int groupId = 1;
        int parentGroup = 1;
        for (int i = 1; i < totalRecords; i++) {
            JSONObject employee = JSONObject.parseObject(template.toString());

            // 确保 EMPLOYEE_NUMBER 唯一
            String employeeNumber;
            do {
                employeeNumber = String.format("%08d", random.nextInt(99999999));
            } while (employeeNumbers.contains(employeeNumber));
            employeeNumbers.add(employeeNumber);

            // 确保 PERSON_ID 唯一
            String personId;
            do {
                personId = String.valueOf(random.nextInt(99999));
            } while (personIds.contains(personId));
            personIds.add(personId);

            if (i % 2 == 0) {
                parentGroup++;
            }
            // 填充数据
            employee.put("EMPLOYEE_NUMBER", employeeNumber);
            employee.put("PERSON_ID", personId);
//            employee.put("BUSINESS_GROUP_ID", String.valueOf(groupId));
            employee.put("ORGANIZATION_ID_PARENT", String.valueOf(groupId));
            employee.put("ORGANIZATION_ID", String.valueOf(parentGroup));
            employee.put("ROWNUM", String.valueOf(i));
            employee.put("NAME", "员工" +i);
            employee.put("IDM_EMP_ACCOUNT", "employee" + i);
            employee.put("SEX", i % 2 == 0 ? "男" : "女"); // 奇偶分配性别
            employee.put("LEAVING_REASON", i % 2 == 0 ? "WT" : "RET"); // 奇偶分配离职原因
            if ((i + 1) == totalRecords) {
                // 增加一天理论无效数据
                employee.put("EFFECTIVE_END_DATE", "2012-12-31T00:00:00");
            }
            groupId++;
            // 添加到数组
            employeeArray.add(employee);
        }
        // 输出生成的测试数据
        JSONObject result = new JSONObject();
        result.put("EMP_ASG_IS13", employeeArray);
        System.out.println(result.toJSONString());
    }

    private static void lizhiUser() {
        // 定义基础数据模板
        JSONObject template = new JSONObject();
        template.put("ACTUAL_TERMINATION_DATE", "2012-12-31T00:00:00.00000000");
        template.put("LEAVING_REASON", "WT");
        template.put("BUSINESS_GROUP_ID", "82");
        template.put("LAST_MODIFY_TM", "2020-02-28T00:00:00.00000000");
        template.put("SEX", "男");
        template.put("PERSON_TYPE", "离职人员");
        template.put("LAST_UPDATE_DATE", "2020-02-28T00:00:00.00000000");
        template.put("MAILSTOP", "example@domain.com");

        // 生成测试数据
        JSONArray employeeArray = new JSONArray();
        Set<String> employeeNumbers = new HashSet<>();
        Set<String> personIds = new HashSet<>();

        int totalRecords = 21; // 需要生成的记录数
        Random random = new Random();
        int groupId = 1;
        for (int i = 1; i < totalRecords; i++) {
            JSONObject employee = JSONObject.parseObject(template.toString());

            // 确保 EMPLOYEE_NUMBER 唯一
            String employeeNumber;
            do {
                employeeNumber = String.format("%08d", random.nextInt(99999999));
            } while (employeeNumbers.contains(employeeNumber));
            employeeNumbers.add(employeeNumber);

            // 确保 PERSON_ID 唯一
            String personId;
            do {
                personId = String.valueOf(random.nextInt(99999));
            } while (personIds.contains(personId));
            personIds.add(personId);

            if (i % 4 == 0) {
                groupId++;
            }
            // 填充数据
            employee.put("EMPLOYEE_NUMBER", employeeNumber);
            employee.put("PERSON_ID", personId);
            employee.put("BUSINESS_GROUP_ID", String.valueOf(groupId));
            employee.put("ROWNUM", String.valueOf(i));
            employee.put("NAME", "员工" + (i + 1));
            employee.put("SEX", i % 2 == 0 ? "男" : "女"); // 奇偶分配性别
            employee.put("LEAVING_REASON", i % 2 == 0 ? "WT" : "RET"); // 奇偶分配离职原因

            // 添加到数组
            employeeArray.add(employee);
        }

        // 输出生成的测试数据
        JSONObject result = new JSONObject();
        result.put("EMP_TERM_CE_GROUP", employeeArray);
        System.out.println(result.toJSONString());
    }


    private static void listOu() {
        // 定义基础数据模板
        JSONObject template = new JSONObject();
        template.put("LEAVING_REASON", "WT");
        template.put("BUSINESS_GROUP_ID", "82");
        template.put("LAST_MODIFY_TM", "2020-02-28T00:00:00.00000000");
        template.put("PARENT_ORG", "武汉有限公司");
        template.put("ACTUAL_TERMINATION_DATE", "2012-12-31T00:00:00.00000000");
        template.put("LAST_UPDATE_DATE", "2020-02-28T00:00:00.00000000");
        template.put("DATE_FROM", "1900-01-01 00:00:00");
        template.put("DATE_TO", "2025-01-01 00:00:00");

        // 生成测试数据
        JSONArray employeeArray = new JSONArray();
        Set<String> employeeNumbers = new HashSet<>();
        Set<String> personIds = new HashSet<>();

        int totalRecords = 11; // 需要生成的记录数
        Random random = new Random();
        int groupId = 1;
        int parentGroup = 1;
        int business = 1;
        for (int i = 1; i < totalRecords; i++) {
            JSONObject employee = JSONObject.parseObject(template.toString());

            // 确保 EMPLOYEE_NUMBER 唯一
            String employeeNumber;
            do {
                employeeNumber = String.format("%08d", random.nextInt(99999999));
            } while (employeeNumbers.contains(employeeNumber));
            employeeNumbers.add(employeeNumber);

            // 确保 PERSON_ID 唯一
            String personId;
            do {
                personId = String.valueOf(random.nextInt(99999));
            } while (personIds.contains(personId));
            personIds.add(personId);

            if (i % 2 == 0) {
                parentGroup++;
                business = parentGroup;
            }
            // 填充数据
            employee.put("EMPLOYEE_NUMBER", employeeNumber);
            employee.put("BUSINESS_GROUP_ID", String.valueOf(business));
            employee.put("ORGANIZATION_ID", String.valueOf(groupId));
            employee.put("ORGANIZATION_ID_PARENT", String.valueOf(parentGroup ));
            employee.put("ROWNUM", String.valueOf(i));
            employee.put("PARENT_ORG", "武汉有限公司"+ i);
            employee.put("ORG_NAME", "组织" + (i + 1));
            employee.put("ORG_NUM", "CE" + (i + 1));
            if ((i + 1) == totalRecords) {
                // 增加一天理论无效数据
                employee.put("DATE_TO", "2012-12-31T00:00:00");
            }
            // 添加到数组
            employeeArray.add(employee);
            groupId++;
            business++;
        }

        // 输出生成的测试数据
        JSONObject result = new JSONObject();
        result.put("EMP_TERM_CE_GROUP", employeeArray);
        System.out.println(result.toJSONString());
    }
}
